package com.dailyblue.smbms.dao;


import com.dailyblue.smbms.bean.Role;
import com.dailyblue.smbms.db.DBManager;
import lombok.SneakyThrows;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RoleDAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    @SneakyThrows
    public Role findRoleById(int id){
        String sql = "select * from smbms_role where id=?";
        conn = DBManager.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        rs = ps.executeQuery();
        if(rs.next()){
            Role role = new Role();
            role.setId(rs.getInt(1));
            role.setRoleCode(rs.getString(2));
            role.setRoleName(rs.getString(3));
            role.setCreatedBy(rs.getInt(4));
            role.setCreationDate(rs.getString(5));
            role.setModifyBy(rs.getInt(6));
            role.setModifyDate(rs.getString(7));
            return role;
        }
        return null;
    }
    @SneakyThrows
    public List<Role> find(){
        List<Role> list = new ArrayList<>();
        String sql = "select * from smbms_role";
        conn = DBManager.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while(rs.next()){
            Role role = new Role();
            role.setId(rs.getInt(1));
            role.setRoleCode(rs.getString(2));
            role.setRoleName(rs.getString(3));
            role.setCreatedBy(rs.getInt(4));
            role.setCreationDate(rs.getString(5));
            role.setModifyBy(rs.getInt(6));
            role.setModifyDate(rs.getString(7));
            list.add(role);
        }
        return list;
    }
}